package com.uphone.magicleafteacher.pro.activity;

import android.app.Activity;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.web.JavaScriptinterface;
import com.radish.baselibrary.web.MyWebViewClient;
import com.radish.baselibrary.web.ProgressWebView;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.StuDetailBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StudentDetailsActivity extends BaseGActivity {
    @BindView(R.id.img_h)
    ImageView imgH;
    @BindView(R.id.img_share)
    ImageView imgShare;
    @BindView(R.id.tool)
    Toolbar tool;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.frame_head)
    FrameLayout frameHead;
    @BindView(R.id.student_name)
    TextView studentName;
    @BindView(R.id.student_age)
    TextView studentAge;
    @BindView(R.id.relative)
    RelativeLayout relative;
    @BindView(R.id.student_evaluate)
    TextView studentEvaluate;
    @BindView(R.id.wb_webview)
    ProgressWebView webView;
    @BindView(R.id.img_x)
    ImageView imgX;
    @IntentData
    private int id;


    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_student_details;
    }

    @Override
    protected void initView() {
        MyApp.apiService(ApiService.class)
                .startDetail("" + id)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        Gson gson = GsonUtils.getGson();
                        StuDetailBean stuDetailBean = gson.fromJson(jsonObject, StuDetailBean.class);
                        StuDetailBean.DetailBean detail = stuDetailBean.getDetail();
                        studentName.setText(detail.getStudentName());
                        studentAge.setText(detail.getAge() + "");
                        studentEvaluate.setText(detail.getStudentComment());
                        if (detail.getGender() == 1) {
                            Glide.with(StudentDetailsActivity.this).load(R.drawable.ic_man).into(imgX);
                        } else {
                            Glide.with(StudentDetailsActivity.this).load(R.drawable.ic_woman).into(imgX);
                        }
                        GlideUtils.getInstance().loadCircleImage(StudentDetailsActivity.this,CommonUtil.getImageUrl(detail.getHeadPhoto()),ivHead);
                        initGeneralWebView(StudentDetailsActivity.this, webView, detail.getIntroduce());
                    }
                }, new FailureConsumer());

    }

    public static void initGeneralWebView(Activity activity, ProgressWebView wv, String content) {
        MyWebViewClient myWebViewClient = new MyWebViewClient();
//        wv.setWebChromeClient(new MyWebChromeClient());
        wv.setWebViewClient(myWebViewClient);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wv.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        wv.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        wv.getSettings().setSupportZoom(false);//是否可以缩放，默认true
        wv.getSettings().setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        wv.getSettings().setUseWideViewPort(false);//设置此属性，可任意比例缩放。大视图模式
        wv.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
//      ******************   缓存设置   *****************
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//
        wv.getSettings().setDomStorageEnabled(false);//DOM Storage
        wv.getSettings().setAppCacheEnabled(false);//是否使用缓存
        wv.getSettings().setDatabaseEnabled(false);   //开启数据库形式存储(false);
//        wv.getSettings().setUserAgentString(MyHttpUtils.getUserAgent(BaseWebActivity.this));//设置用户代理，一般不用
        wv.getSettings().setUserAgentString("learnenglish");//设置用户代理，一般不用
        wv.addJavascriptInterface(new JavaScriptinterface(activity, wv), "android");
        wv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        Map<String, String> extraHeaders = new HashMap<String, String>();
        extraHeaders.put("device", "android");

        String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script>";
//        wv.loadData(varjs + content, "text/html", "UTF-8");
        wv.loadDataWithBaseURL(null, varjs + content, "text/html", "UTF-8", null);
    }

    @Override
    protected void initTitle() {

    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_h, R.id.img_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.img_h:
                finish();
                break;
            //分享
            case R.id.img_share:

                break;
        }
    }
}
